﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Protell.DAL;
using Protell.Model;
using Protell.Model.IRepository;
using System.Collections.ObjectModel;

namespace Protell.ViewModel
{
    public class AccionProtocoloAddViewModel : ViewModelBase
    {        
        // ***************************** ***************************** *****************************
        // Repository.
        private IAccionProtocolo _AccionProtocoloRepository;

        public AccionProtocoloModel AccionProtocolo
        {
            get { return _AccionProtocolo; }
            set
            {
                if (_AccionProtocolo != value)
                {
                    _AccionProtocolo = value;
                    OnPropertyChanged(AccionProtocoloPropertyName);
                }
            }
        }
        private AccionProtocoloModel _AccionProtocolo;
        public const string AccionProtocoloPropertyName = "AccionProtocolo";

        private ICondPro _CondProRepository;
        public ObservableCollection<CondProModel> CondPros
        {
            get { return _CondPros; }
            set
            {
                if (_CondPros != value)
                {
                    _CondPros = value;
                    OnPropertyChanged(CondProsPropertyName);
                }
            }
        }
        private ObservableCollection<CondProModel> _CondPros;
        public const string CondProsPropertyName = "CondPros";

        private IEstructura _EstructuraRepository;

        public ObservableCollection<EstructuraModel> Estructuras
        {
            get { return _Estructuras; }
            set
            {
                if (_Estructuras != value)
                {
                    _Estructuras = value;
                    OnPropertyChanged(EstructurasPropertyName);
                }
            }
        }
        private ObservableCollection<EstructuraModel> _Estructuras;
        public const string EstructurasPropertyName = "Estructuras";


        // ***************************** ***************************** *****************************
        // auxiliar.
        public AccionProtocoloModel CheckSave
        {
            get { return _CheckSave; }
            set
            {
                if (_CheckSave != value)
                {
                    _CheckSave = value;
                    OnPropertyChanged(CheckSavePropertyName);
                }
            }
        }
        private AccionProtocoloModel _CheckSave;
        public const string CheckSavePropertyName = "CheckSave";


        // ***************************** ***************************** *****************************
        // label, para desplegar si el elemento existe o no.
        public string ElementExists
        {
            get { return _ElementExists; }
            set
            {
                if (_ElementExists != value)
                {
                    _ElementExists = value;
                    OnPropertyChanged(ElementExistsPropertyName);
                }
            }
        }
        private string _ElementExists;
        public const string ElementExistsPropertyName = "ElementExists";


        // ***************************** ***************************** *****************************
        // boton de guardar.
        public RelayCommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new RelayCommand(p => this.AttemptSave(), p => this.CanSave());
                }
                return _SaveCommand;
            }
        }
        private RelayCommand _SaveCommand;
        public bool CanSave()
        {
            bool _CanSave = false;

            if ((this._AccionProtocolo != null) || !String.IsNullOrEmpty(this._AccionProtocolo.AccionProtocoloDesc))
            {
                _CanSave = true;
                this._CheckSave = this._AccionProtocoloRepository.GetAccionProtocoloMOD(this._AccionProtocolo);

                if (this._CheckSave != null)
                {
                    _CanSave = false;
                    ElementExists = "El elemento ya existe.";

                }
                else
                {
                    _CanSave = true;
                    ElementExists = "";
                }
            }

            return _CanSave;
        }
        public void AttemptSave()
        {
            //logica para guardar el registro
            this._AccionProtocoloRepository.InsertAccionProtocolo(this._AccionProtocolo);
        }


        // ***************************** ***************************** *****************************
        // constructor
        public AccionProtocoloAddViewModel()
        {
            this._AccionProtocoloRepository = new Protell.DAL.Repository.AccionProtocoloRepository();
            this._CondProRepository = new Protell.DAL.Repository.CondProRepository();
            this._EstructuraRepository = new Protell.DAL.Repository.EstructuraRepository();

            this._AccionProtocolo = new AccionProtocoloModel()
            {
                IdAccionProtocolo = new UNID().getNewUNID(),
                IsActive = true
            };

            this.LoadInfo();
        }

        public void LoadInfo()
        {
            this.CondPros = this._CondProRepository.GetCondPros() as ObservableCollection<CondProModel>;
            this.Estructuras = this._EstructuraRepository.GetEstructuras() as ObservableCollection<EstructuraModel>;
            
        }
    }
}
